package com.example.manging;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Component(value = "scoreManage")
public class ScoreManageImpl implements ScoreManage{
        @Autowired
        JdbcTemplate jdbcTemplate;
 @Override
 public void addScore(Score score) {
            String sql = "insert into scores (number,Chinese,math,English) value (?,?,?,?)";
            Object obj =   new Object[] {
                    score.getNumber(),
                    score.getChinser(),
                    score.getMath(),
                    score.getEnglish()
            };//当前的ogject无法使用
            int num = jdbcTemplate.update(sql,
                    score.getNumber(),
                    score.getChinser(),
                    score.getMath(),
                    score.getEnglish()
            );
            if (num <= 0){
                System.out.println("添加失败");
            }
        }

        //修改学科分数
        public void updateScore(String number,  Double score) {
            String sql = "update scores set Chinese = ?  where number = ? " ;
            int num = jdbcTemplate.update(sql, score, number );
            if (num <= 0){
                System.out.println("修改失败");
            }
        }

        @Override
        public void queryScoreByNumber(String number) {
            String sql = "select * from score where number = ? ";
            RowMapper<Score> rowMapper;
            rowMapper = new BeanPropertyRowMapper<>(Score.class);
            Score score = jdbcTemplate.queryForObject(sql, rowMapper, number);
            System.out.println(score);

        }

        @Override
        public List<Score> queryAllScore() {
            String sql = "select * from scores";
            List<Score> score = jdbcTemplate.query(sql,
                    new BeanPropertyRowMapper<>(Score.class));
            return score;
        };
    }
